package org.demo.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 通用处理事件监听器
 *
 * @author yexi
 */
@Getter
public class CommonEventListener<T> extends AnalysisEventListener<T> {

    private final List<T> dataList = new ArrayList<>();

    @Override
    public void invoke(T data, AnalysisContext context) {
        System.out.println("读取到内容: " + data);
        // 每次读取到数据后怎么处理
        dataList.add(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        ReadRowHolder readRowHolder = context.readRowHolder();
        System.out.println("已全部导入完毕");
        System.out.println("最后读取到的内容: " + readRowHolder.getCellMap());
        System.out.println("最后读取到第几行: " + readRowHolder.getRowIndex());
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("读取到表头: " + headMap);
        ReadRowHolder readRowHolder = context.readRowHolder();
        System.out.println("现在读取到的东西: " + readRowHolder.getCellMap());
        System.out.println("现在读取到的是第几行: " + readRowHolder.getRowIndex());
    }

    @Override
    public void extra(CellExtra extra, AnalysisContext context) {
        System.out.println("返回的额外信息: " + extra);
    }

    @Override
    public boolean hasNext(AnalysisContext context) {
        return super.hasNext(context);
    }

}
